<?php
/**
 * 检查文件上传配置
 */

echo "<h2>PHP文件上传配置检查</h2>\n";

// 检查PHP配置
$configs = [
    'upload_max_filesize' => ini_get('upload_max_filesize'),
    'post_max_size' => ini_get('post_max_size'),
    'max_file_uploads' => ini_get('max_file_uploads'),
    'memory_limit' => ini_get('memory_limit'),
    'max_execution_time' => ini_get('max_execution_time'),
    'max_input_time' => ini_get('max_input_time'),
];

echo "<table border='1' style='border-collapse: collapse;'>\n";
echo "<tr><th>配置项</th><th>当前值</th><th>建议值</th><th>状态</th></tr>\n";

$recommendations = [
    'upload_max_filesize' => '10M',
    'post_max_size' => '20M',
    'max_file_uploads' => '20',
    'memory_limit' => '256M',
    'max_execution_time' => '300',
    'max_input_time' => '300',
];

foreach ($configs as $key => $value) {
    $recommended = $recommendations[$key];
    $status = '✓';
    $color = 'green';
    
    // 简单的大小比较（仅针对带M的配置）
    if (strpos($value, 'M') !== false && strpos($recommended, 'M') !== false) {
        $currentSize = (int)str_replace('M', '', $value);
        $recommendedSize = (int)str_replace('M', '', $recommended);
        if ($currentSize < $recommendedSize) {
            $status = '⚠️';
            $color = 'orange';
        }
    }
    
    echo "<tr>";
    echo "<td>{$key}</td>";
    echo "<td>{$value}</td>";
    echo "<td>{$recommended}</td>";
    echo "<td style='color: {$color};'>{$status}</td>";
    echo "</tr>\n";
}

echo "</table>\n";

// 检查ThinkAdmin配置
echo "<h2>ThinkAdmin系统配置</h2>\n";

try {
    // 引入ThinkAdmin
    require_once __DIR__ . '/vendor/autoload.php';
    $app = new \think\App();
    $app->initialize();
    
    // 检查系统配置
    $allowExts = \think\admin\service\SystemService::get('storage.allow_exts', '');
    echo "<p><strong>允许的文件类型:</strong> {$allowExts}</p>\n";
    
    // 检查是否包含图片格式
    $imageExts = ['jpg', 'jpeg', 'png', 'gif'];
    $allowedExts = explode(',', strtolower($allowExts));
    $missingExts = array_diff($imageExts, $allowedExts);
    
    if (empty($missingExts)) {
        echo "<p style='color: green;'>✓ 图片格式支持完整</p>\n";
    } else {
        echo "<p style='color: red;'>⚠️ 缺少图片格式支持: " . implode(', ', $missingExts) . "</p>\n";
    }
    
} catch (\Throwable $e) {
    echo "<p style='color: red;'>无法检查ThinkAdmin配置: " . $e->getMessage() . "</p>\n";
}

echo "<h2>建议操作</h2>\n";
echo "<ol>\n";
echo "<li>如果PHP配置不足，请修改 php.ini 文件中的相关配置</li>\n";
echo "<li>如果缺少图片格式支持，请在后台 系统管理 → 系统配置 → 存储配置 中添加</li>\n";
echo "<li>修改后重启Web服务器</li>\n";
echo "</ol>\n";
?>
